Handling the wivents of widgets using:
Video Tutorial: http://youtu.be/hXS4xhyx0a8
In [1]:
from IPython.html import widgets
In [2]:
def print_msg(widget):
print "You have clicked (%s)" % widget.description
container = widgets.ContainerWidget()
button_1 = widgets.ButtonWidget(description="Click Me")
button_2 = widgets.ButtonWidget(description="Don't Click Me")
button_1.on_click(print_msg)
button_2.on_click(print_msg)
container.children = [button_1, button_2]
container
In [3]:
container = widgets.ContainerWidget()
control_1 = widgets.FloatTextWidget(description="X: ")
control_2 = widgets.FloatTextWidget(description="Y: ")
control_3 = widgets.SelectWidget(description= "Operation", values=["+","-","*","/"])
control_4 = widgets.LatexWidget(value="Formula: NA")
control_5 = widgets.LatexWidget(value="Result: 0.0")
container.children = [control_1, control_2, control_3, control_4, control_5]
def calculate_form(name):
# Retreive values from form
form = container.children
X = form[0].value
Y = form[1].value
operation = form[2].value
# Build Fomula
formula = str(X) + operation + str(Y)
form[3].value = "Formula: %s" % formula
# Check if Divive by Zero
if Y == 0.0 and operation == "/":
form[4].value = "Div by Zero"
else:
# Evaluate fumula
form[4].value = str(eval(formula))
print "%s = %s" % (formula, control_5.value)
# Add Handlers
control_1.on_trait_change(calculate_form, "value")
control_2.on_trait_change(calculate_form, "value")
control_3.on_trait_change(calculate_form, "value")
container
In [4]:
container